<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="体系分类" prop="serviceId">
        <!--        <el-select v-model="queryParams.serviceId" filterable placeholder="请选择所属分类">-->
        <!--          <el-option-->
        <!--            v-for="dict in centerList"-->
        <!--            :key="dict.id"-->
        <!--            :label="dict.serviceName+'-'+dict.levelNumber+'级'"-->
        <!--            :value="parseInt(dict.id)"-->
        <!--          ></el-option>-->
        <!--        </el-select>-->
        <treeselect style="width: 300px" v-model="queryParams.ids" :options="centerOptions[0].children"
                    :normalizer="normalizer"
                    :multiple="true"
                    placeholder="请选择所属分类"/>
        <!--        <treeselect style="width: 300px" v-model="queryParams.serviceId" :options="centerOptions[0].children"-->
        <!--                    :normalizer="normalizer"-->
        <!--                    placeholder="请选择所属分类"/>-->
      </el-form-item>
      <el-form-item label="发布用户" prop="releaseName">
        <el-input
          v-model="queryParams.releaseName"
          placeholder="请输入发布用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间" prop="createTime">
        <el-date-picker clearable
                        v-model="queryParams.createTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择">
        </el-date-picker>
      </el-form-item>
      <!--      <el-form-item label="审核状态" prop="state">-->
      <!--        <el-select v-model="queryParams.state" placeholder="请选择审核状态">-->
      <!--          <el-option-->
      <!--            v-for="dict in dict.type.audit_status"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="parseInt(dict.value)"-->
      <!--          ></el-option>-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="排序" prop="sort">
        <el-select v-model="queryParams.sort" placeholder="请选择">
          <el-option
            v-for="dict in sortType"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="注册地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实际地址" prop="address2">
        <el-input
          v-model="queryParams.address2"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主营产品" prop="intro">
        <el-input
          v-model="queryParams.intro"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业标签" prop="enterpriseLabel">
        <el-select v-model="queryParams.enterpriseLabel" placeholder="请选择">
          <el-option
            v-for="dict in dict.type.enterprise_label"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分配用户" prop="provinceId">
        <el-select v-model="queryParams.provinceId" placeholder="请选择">
          <el-option
            v-for="dict in fenpeiList"
            :key="dict.userId"
            :label="dict.nickName"
            :value="parseInt(dict.userId)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="dr">
        <el-select v-model="queryParams.dr" placeholder="请选择">
          <el-option
            v-for="dict in dict.type.audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['information:serviceArticle:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['information:serviceArticle:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['information:serviceArticle:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="serviceArticleList" @selection-change="handleSelectionChange" height="650"
              fixed>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="企业名称" align="center" prop="title"/>
      <el-table-column label="发布用户" align="center" prop="releaseName"/>
      <el-table-column label="发布时间" align="center" prop="createTime"/>
      <el-table-column label="体系分类" align="center" prop="serviceId" show-overflow-tooltip>
        <template slot-scope="scope">
          <div v-for="team in scope.row.serviceId.split(',')" style="display: inline-block">
            <div v-for="item in centerList" v-if="item.id == team"  style="display: inline-block">
              <span>{{ item.serviceName }}-{{ item.levelNumber }}级<span v-if="scope.row.serviceId.split(',')[scope.row.serviceId.split(',').length-1]!=team">,</span></span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="封面" align="center" prop="cover" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.cover" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="链接类型" align="center" prop="linkType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.link_type" :value="scope.row.linkType"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="dr">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audit_status" :value="scope.row.dr"/>
        </template>
      </el-table-column>
      <el-table-column width="220" fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleLook(scope.row.id)"
          >预览
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['information:serviceArticle:edit']"
          ><span v-if="$store.getters.userId===scope.row.sort">编辑</span>
            <span v-if="$store.getters.userId!==scope.row.sort">查看</span>
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['information:serviceArticle:remove']"
          >删除
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleAudit(scope.row)"-->
          <!--            v-hasPermi="['information:serviceArticle:edit']"-->
          <!--            v-if="scope.row.state !== 3"-->
          <!--          >审核-->
          <!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="759px" :close-on-click-modal='false' append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="企业名称" prop="title">
          <el-input class="kind" v-model="form.title" placeholder="企业名称"/>
        </el-form-item>
        <el-form-item label="企业标签">
          <el-checkbox v-model="form.enterpriseLabel" v-for="item in dict.type.enterprise_label" :label="item.value"
                       :key="item.value">{{ item.value }}
          </el-checkbox>
        </el-form-item>
        <el-form-item label="企业封面" prop="cover">
          <image-upload :limit="1" v-model="form.cover"/>
        </el-form-item>
        <el-form-item label="发布用户" prop="releaseName">
          <el-input class="kind" v-model="form.releaseName" placeholder="请输入发布用户"/>
        </el-form-item>
        <el-form-item label="需求分类">
          <!--          <el-select class="kind" v-model="form.serviceId" filterable placeholder="请选择所属分类">-->
          <!--            <el-option-->
          <!--              v-for="dict in centerList"-->
          <!--              :key="dict.id"-->
          <!--              :label="dict.serviceName+'-'+dict.levelNumber+'级'"-->
          <!--              :value="parseInt(dict.id)"-->
          <!--            ></el-option>-->
          <!--          </el-select>-->
          <!--          <treeselect style="width: 300px" v-model="form.ids" :options="centerOptions[0].children"-->
          <!--                      :multiple="true"-->
          <!--                      :normalizer="normalizer"-->
          <!--                      placeholder="请选择所属分类"/>-->
          <el-tree
            class="tree-border kind"
            :data="centerOptions[0].children"
            show-checkbox
            ref="menu"
            node-key="id"
            :check-strictly="false"
            empty-text="加载中，请稍候"
            :props="defaultProps"
          ></el-tree>
        </el-form-item>
        <el-form-item label="是否拥有自主专利" prop="isIndependence">
          <el-radio v-model="form.isIndependence" label="0">否</el-radio>
          <el-radio v-model="form.isIndependence" label="1">是</el-radio>
        </el-form-item>
        <el-form-item prop="note" v-if="form.isIndependence=='1'">
          <el-input class="kind" type="textarea" maxlength="500" show-word-limit v-model="form.note"
                    placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="主营产品" prop="intro">
          <el-input class="kind" v-model="form.intro" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="链接类型" prop="linkType">
          <el-select class="kind" @change="changeType" v-model="form.linkType" placeholder="请选择链接类型">
            <el-option
              v-for="dict in dict.type.link_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.linkType===1" label="企业简介">
          <MyEditor class="kind" v-model="form.linkVal" placeholder="概括公司优势、业务范围、发展前景等。"
                    :min-height="192"/>
        </el-form-item>
        <el-form-item v-if="form.linkType===2" label="企业简介">
          <el-input class="kind" v-model="form.linkVal"/>
        </el-form-item>
        <el-form-item label="资质" v-if="open" prop="qualifications">
          <MyEditor class="kind" v-model="form.qualifications" placeholder="简要介绍产品特点、优势、收益等。"
                    :min-height="192"/>
        </el-form-item>
        <el-form-item label="业务覆盖地区" prop="businessAddress">
          <el-input class="kind" v-model="form.businessAddress" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="企业注册地址" prop="address">
          <!--          <el-input v-model="form.provinceId" placeholder="请输入省份ID" />-->
          <el-select v-model="form.province" placeholder="请选择省份"
                     @change="getCityList(form.province)" style="width: 30%">
            <el-option
              v-for="dict in provinces"
              :key="dict.name"
              :label="dict.name"
              :value="dict.name"
            />
          </el-select>
          <el-select v-model="form.city" placeholder="请选择城市"
                     :disabled="form.province==null||form.province===''" @change="getAreaList(form.city)"
                     style="width: 30%">
            <el-option
              v-for="dict in citys"
              :key="dict.name"
              :label="dict.name"
              :value="dict.name"
            />
          </el-select>
          <el-select v-model="form.area" placeholder="请选择区域" :disabled="form.city==null||form.city===''"
                     style="width: 30%" clearable>
            <el-option
              v-for="dict in areas"
              :key="dict.name"
              :label="dict.name"
              :value="dict.name"
            />
          </el-select>
          <el-input class="kind" v-model="form.address" type="textarea" maxlength="200" show-word-limit
                    placeholder="请输入详细地址"/>
        </el-form-item>
        <el-form-item label="企业实际地址" prop="address2">
          <!--          <el-input v-model="form.provinceId" placeholder="请输入省份ID" />-->
          <el-select v-model="form.province2" placeholder="请选择省份"
                     @change="getCityList3(form.province2)" style="width: 30%">
            <el-option
              v-for="dict in provinces2"
              :key="dict.name"
              :label="dict.name"
              :value="dict.name"
            />
          </el-select>
          <el-select v-model="form.city2" placeholder="请选择城市"
                     :disabled="form.province2==null||form.province2===''" @change="getAreaList2(form.city2)"
                     style="width: 30%">
            <el-option
              v-for="dict in citys2"
              :key="dict.name"
              :label="dict.name"
              :value="dict.name"
            />
          </el-select>
          <el-select v-model="form.area2" placeholder="请选择区域" :disabled="form.city2==null||form.city2===''"
                     style="width: 30%" clearable>
            <el-option
              v-for="dict in areas2"
              :key="dict.name"
              :label="dict.name"
              :value="dict.name"
            />
          </el-select>
          <el-input class="kind" v-model="form.address2" type="textarea" maxlength="200" show-word-limit
                    placeholder="请输入详细地址"/>
        </el-form-item>
        <el-form-item label="注册资金" prop="fund">
          <el-input class="kind" v-model="form.fund" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="职工人数" prop="workers">
          <el-input class="kind" v-model="form.workers" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="图片资源" prop="pictures">
          <image-upload v-model="form.pictures"/>
        </el-form-item>
        <el-form-item label="下载资源" prop="downloadUrl">
          <file-upload v-model="form.downloadUrl"/>
        </el-form-item>
        <el-form-item label="分配用户" prop="provinceId">
          <el-select class="kind" v-model="form.provinceId" placeholder="请选择分配用户">
            <el-option
              v-for="dict in fenpeiList"
              :key="dict.userId"
              :label="dict.nickName"
              :value="parseInt(dict.userId)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核备注" v-if="form.dr!=0" prop="remark">
          <el-input class="kind" disabled type="textarea" v-model="form.remark"/>
        </el-form-item>
      </el-form>
      <div>
        <el-divider content-position="center">联系人信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button icon="el-icon-plus" size="mini" type="primary" @click="handleAddUserList">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button icon="el-icon-delete" size="mini" type="danger" @click="handleDeleteUserList">删除
            </el-button>
          </el-col>
        </el-row>
        <el-table ref="user" :cell-style="{padding:'0px'}"
                  :data="userList"
                  :row-class-name="rowUserListIndex"
                  @selection-change="handleUserListSelectionChange"
        >
          <el-table-column align="center" type="selection" width="50"/>
          <el-table-column align="center" label="序号" prop="index" width="150"/>
          <el-table-column label="联系人" prop="specsId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.contactName" placeholder="请输入联系人"/>
            </template>
          </el-table-column>
          <el-table-column label="职位" prop="photoGallery" width="200">
            <template slot-scope="scope">
              <el-input v-model="scope.row.contactAddress" placeholder="请输入职位"/>
            </template>
          </el-table-column>
          <el-table-column label="联系方式" prop="craftId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.contactPhone" placeholder="请输入联系方式"/>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="$store.getters.userId===form.sort" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 审核 -->
    <el-dialog title="审核" :visible.sync="buildingAudit" close-on-click-modal center width="750px">
      <el-form :model="form" label-width="130px">
        <el-form-item label="审核备注" prop="remark">
          <el-input class="kind" type="textarea" v-model="form.remark"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" v-if="form.dr===0" @click="submitCheck(3)">审核通过</el-button>
        <el-button type="success" v-if="form.dr===3" @click="submitCheck(1)">审核通过</el-button>
        <span style="width: 5%;display: inline-block"></span>
        <el-button type="danger" v-if="form.dr===0" @click="submitCheck(2)">审核驳回</el-button>
        <el-button type="danger" v-if="form.dr===3" @click="submitCheck(4)">审核驳回</el-button>
        <span style="width: 5%;display: inline-block"></span>
        <el-button type="info" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--    <el-dialog title="审核" :visible.sync="open2" width="500px" append-to-body center>-->
    <!--      <el-form ref="form" :model="form" :rules="rules" label-width="130px">-->
    <!--        <el-form-item label="审核状态" prop="state">-->
    <!--          <el-select v-model="form.state" placeholder="请选择审核状态">-->
    <!--            <el-option-->
    <!--              v-for="dict in dict.type.audit_status"-->
    <!--              :key="dict.value"-->
    <!--              :label="dict.label"-->
    <!--              :value="parseInt(dict.value)"-->
    <!--            ></el-option>-->
    <!--          </el-select>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="文章来源" prop="note">-->
    <!--          <el-input style="width: 213px" v-model="form.note" placeholder="请输入文章来源"/>-->
    <!--        </el-form-item>-->
    <!--      </el-form>-->
    <!--      <div slot="footer" class="dialog-footer">-->
    <!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
    <!--        <el-button @click="cancel">取 消</el-button>-->
    <!--      </div>-->
    <!--    </el-dialog>-->
  </div>
</template>

<script>
import {
  addServiceArticle,
  delServiceArticle,
  getServiceArticle,
  listServiceArticle,
  updateServiceArticle
} from "@/api/information/serviceArticle";
import {listCenter} from "@/api/information/center";
import MyEditor from "@/components/wangEdiot/index.vue";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listCity} from "@/api/business/businessIntro";
import {listUser} from "@/api/system/user";

export default {
  name: "ServiceArticle",
  components: {Treeselect, MyEditor},
  dicts: ['link_type', 'audit_status', 'enterprise_label', 'audit_status'],
  data() {
    return {
      defaultProps: {
        children: "children",
        label: "serviceName"
      },
      fenpeiList: [],
      provinces: null,
      citys: null,
      areas: null,
      provinces2: null,
      citys2: null,
      areas2: null,
      buildingAudit: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业信息表格数据
      serviceArticleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      sortType: [
        {
          value: 0,
          label: "按时间倒序"
        },
        {
          value: 1,
          label: "按时间正序"
        },
      ],
      // 查询参数
      queryParams: {
        id: null,
        pageNum: 1,
        pageSize: 10,
        title: null,
        releaseName: null,
        releaseTime: null,
        linkType: null,
        linkVal: null,
        intro: null,
        downloadUrl: null,
        serviceId: null,
        note: null,
        sort: this.$store.getters.userId,
        state: null,
        dr: null,
        cover: null,
        num: null,
        recom: null,
        createTime: null,
        ids: [],
        address: null,
        address2: null,
        enterpriseLabel: null,
        provinceId: null
      },
      centerList: [],
      checkedUserList: [],
      // 体系分类树选项
      centerOptions: [],
      userList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          {required: true, message: "企业名称不能为空", trigger: "blur"},
          {max: 100, message: '长度必须小于 100', trigger: 'blur'}
        ],
        releaseName: [
          {required: true, message: "发布用户不能为空", trigger: "blur"}
        ],
        releaseTime: [
          {required: true, message: "发布时间不能为空", trigger: "blur"}
        ],
        linkType: [
          {required: true, message: "链接类型不能为空", trigger: "change"}
        ],
        linkVal: [
          {required: true, message: "内容不能为空", trigger: "blur"}
        ],
        businessAddress: [
          {required: true, message: "不能为空", trigger: "blur"}
        ],
        address: [
          {required: true, message: "不能为空", trigger: "blur"}
        ],
        intro: [
          {required: true, message: "简介不能为空", trigger: "blur"},
          {max: 100, message: '长度必须小于 100', trigger: 'blur'}
        ],
        ids: [
          {required: false, message: "体系分类不能为空", trigger: "change"}
        ],
        sort: [
          {required: true, message: "排序不能为空", trigger: "blur"}
        ],
        state: [
          {required: true, message: "状态不能为空", trigger: "blur"}
        ],
        cover: [
          {required: true, message: "封面不能为空", trigger: "blur"}
        ],
        num: [
          {required: true, message: "浏览次数不能为空", trigger: "blur"}
        ],
        fund: [
          {required: true, message: "不能为空", trigger: "blur"}
        ],
        workers: [
          {required: true, message: "不能为空", trigger: "blur"}
        ],
        note: [
          {required: true, message: "不能为空", trigger: "blur"},
          {max: 500, message: '长度必须小于 500', trigger: 'blur'}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getCenter();
    this.getprovinceList();
    listUser(this.addDateRange({
      pageNum: 1,
      pageSize: 9999999
    }, [])).then(response => {
        this.fenpeiList = response.rows;
        this.fenpeiList = this.fenpeiList.filter(t => t.userId != 1)
      }
    )
  },
  methods: {
    // 所有菜单节点数据
    getMenuAllCheckedKeys() {
      // 目前被选中的菜单节点
      let checkedKeys = this.$refs.menu.getCheckedKeys();
      // 半选中的菜单节点
      let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
    /** 查询省列表 */
    getprovinceList() {
      var parentId = 0;
      listCity(parentId).then(res => {
        // console.log("AAA",res)
        this.provinces = res.data;
        this.provinces2 = res.data;
      })
    },
    //查询市列表
    getCityList(provinceId) {
      //console.log("BBB",provinceId)
      var id = null;

      for (let i = 0; i < this.provinces.length; i++) {
        if (provinceId === this.provinces[i].name) {
          id = this.provinces[i].id
        }
      }
      listCity(id).then(res => {
        this.citys = res.data;
      })
    },
    //查询市列表
    getCityList2(provinceId) {
      //console.log("BBB",provinceId)
      var id = null;

      for (let i = 0; i < this.provinces.length; i++) {
        if (provinceId === this.provinces[i].name) {
          id = this.provinces[i].id
        }
      }
      listCity(id).then(res => {
        this.citys = res.data;
        if (this.form.city != null && this.form.city !== "" && this.form.city !== "null") {
          this.getAreaList(this.form.city)
        }
      })
    },
    //查询区列表
    getAreaList(cityId) {
      var id = null;
      for (let i = 0; i < this.citys.length; i++) {
        if (cityId === this.citys[i].name) {
          id = this.citys[i].id
        }
      }
      listCity(id).then(res => {
        this.areas = res.data;
        console.log(res.data)
      })
    },
    //查询市列表
    getCityList3(provinceId) {
      //console.log("BBB",provinceId)
      var id = null;

      for (let i = 0; i < this.provinces2.length; i++) {
        if (provinceId === this.provinces2[i].name) {
          id = this.provinces2[i].id
        }
      }
      listCity(id).then(res => {
        this.citys2 = res.data;
      })
    },
    //查询市列表
    getCityList4(provinceId) {
      //console.log("BBB",provinceId)
      var id = null;

      for (let i = 0; i < this.provinces2.length; i++) {
        if (provinceId === this.provinces2[i].name) {
          id = this.provinces2[i].id
        }
      }
      listCity(id).then(res => {
        this.citys = res.data;
        if (this.form.city2 != null && this.form.city2 !== "" && this.form.city2 !== "null") {
          this.getAreaList2(this.form.city2)
        }
      })
    },
    //查询区列表
    getAreaList2(cityId) {
      var id = null;
      for (let i = 0; i < this.citys2.length; i++) {
        if (cityId === this.citys2[i].name) {
          id = this.citys2[i].id
        }
      }
      listCity(id).then(res => {
        this.areas2 = res.data;
        console.log(res.data)
      })
    },
    handleLook(id) {
      open(`http://120.46.220.152/#/pages/serviceHall/details?id=` + id + `&name=预览`)
    },
    /** 提交审核 */
    submitCheck(state) {
      updateServiceArticle({
        id: this.form.id,
        dr: state,
        remark: this.form.remark
      }).then(response => {
        this.$modal.msgSuccess("审核成功");
        this.buildingAudit = false;
        this.getList();
      });
    },
    /** 审核 */
    spaceAudit(row) {
      this.reset();
      this.form.id = row.id
      this.form.dr = row.dr
      this.form.remark = row.remark
      this.buildingAudit = true;
    },
    changeType() {
      this.form.linkVal = null
    },
    getCenter() {
      this.loading = true;
      listCenter().then(response => {
        this.centerList = response.data;

        this.centerOptions = [];
        const data = {id: 0, serviceName: '顶级节点', children: []};
        data.children = this.handleTree(response.data, "id", "parentId");
        this.centerOptions.push(data);
        // this.centerList = this.handleTree(response.data, "id", "parentId");
        this.loading = false;
      })
    },
    /** 转换体系分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id + "",
        label: node.serviceName,
        children: node.children
      };
    },
    /** 查询企业信息列表 */
    getList() {
      this.loading = true;
      listServiceArticle(this.queryParams).then(response => {
        this.serviceArticleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open2 = false;
      this.buildingAudit = false;
      this.reset();
    },
    // 表单重置
    reset() {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      this.form = {
        id: null,
        title: null,
        releaseName: null,
        releaseTime: null,
        linkType: null,
        linkVal: null,
        intro: null,
        downloadUrl: null,
        serviceId: null,
        note: null,
        sort: this.$store.getters.userId,
        state: null,
        dr: 0,
        createTime: null,
        updateTime: null,
        cover: null,
        num: null,
        provinceId: null,
        cityId: null,
        areaId: null,
        recom: null,
        remark: null,
        pictures: null,
        qualifications: null,
        address: null,
        fund: null,
        workers: null,
        ids: [],
        userList: '',
        enterpriseLabel: [],
        province: null,
        city: null,
        area: null,
        province2: null,
        city2: null,
        area2: null,
        address2: null,
        businessAddress: null,
        isIndependence: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.ids = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.linkType = 1
      this.title = "添加企业信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getServiceArticle(id).then(response => {
        this.form = response.data;
        if (this.form.enterpriseLabel != null) {
          this.form.enterpriseLabel = this.form.enterpriseLabel.split(",")
        }
        if (this.form.province != null && this.form.province !== "") {
          this.getCityList2(this.form.province)
        }
        if (this.form.province2 != null && this.form.province2 !== "") {
          this.getCityList4(this.form.province2)
        }
        this.form.ids.forEach((v) => {
          this.$nextTick(() => {
            this.$refs.menu.setChecked(v, true, false);
          })
        });
        // this.form.ids = this.form.ids.filter(item => {
        //   let a = false
        //   this.centerList.forEach(i => {
        //     if (i.id == item) {
        //       a = true
        //     }
        //   })
        //   return a
        // })
        this.userList = response.data.userList
        this.open = true;
        if (this.$store.getters.userId !== this.form.sort) {
          this.title = "企业信息详情";
        } else {
          this.title = "修改企业信息";
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.getMenuAllCheckedKeys());
          if (this.getMenuAllCheckedKeys().length==0){
            this.$modal.msgError("需求分类不能为空");
            return
          }
          this.form.ids = this.getMenuAllCheckedKeys()
          this.form.userList = this.userList
          if (this.userList.length < 1) {
            this.$modal.msgError("至少需要一组联系人");
            return
          }
          if (this.form.enterpriseLabel != null) {
            this.form.enterpriseLabel = this.form.enterpriseLabel.join(",")
          }
          if (this.form.isIndependence == '0') {
            this.form.note = ''
          }
          if (this.form.id != null) {
            this.form.dr = 0
            updateServiceArticle(this.form).then(response => {
              if (this.open) {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
              }
              if (this.open2) {
                this.$modal.msgSuccess("审核成功");
                this.open2 = false;
              }
              this.getList();
            });
          } else {
            this.form.state = 3
            addServiceArticle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除企业信息编号为"' + ids + '"的数据项？').then(function () {
        return delServiceArticle(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      this.reset();
      const id = row.id
      getServiceArticle(id).then(response => {
        this.form = response.data;
        this.open2 = true;
      });
    },
    /** 推荐按钮操作 */
    handleRecom(row) {
      const id = row.id
      if (row.recom === 0) {
        this.$modal.confirm('是否确认推荐企业信息编号为"' + id + '"的数据项？').then(function () {
          row.recom = 1
          return updateServiceArticle(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("推荐成功");
        }).catch(() => {
        });
      } else {
        this.$modal.confirm('是否确认解除推荐企业信息编号为"' + id + '"的数据项？').then(function () {
          row.recom = 0
          return updateServiceArticle(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("解除推荐成功");
        }).catch(() => {
        });
      }

    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('information/serviceArticle/export', {
        ...this.queryParams
      }, `serviceArticle_${new Date().getTime()}.xlsx`)
    },
    /** 定制品类与规格和工艺绑定管理序号 */
    rowUserListIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 定制品类与规格和工艺绑定管理添加按钮操作 */
    handleAddUserList() {
      let obj = {};
      obj.contactName = "";//联系人
      obj.contactPhone = "";//联系方式
      obj.contactAddress = "";//职位

      if (this.userList === undefined || this.userList == null) {
        this.userList = []
      }
      this.userList.push(obj);
    },
    /** 定制品类与规格和工艺绑定管理删除按钮操作 */
    handleDeleteUserList() {
      if (this.checkedUserList.length == 0) {
        this.$modal.msgError("请先选择要删除的联系人信息");
      } else {
        const userList = this.userList;
        const checkedUserList = this.checkedUserList;
        this.userList = userList.filter(function (item) {
          return checkedUserList.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleUserListSelectionChange(selection) {
      this.checkedUserList = selection.map(item => item.index)
    },
  }
};
</script>
<style scoped>
.kind {
  width: 90%;
}
</style>
